/**
 * Created by Administrator on 2016/7/14.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog'
], function( $, common, ready, template, Collection, Model, dialog ) {

    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.event();
            this.fetch();
        },

        fetch: function(_args) {
            var args = _args || {};
            args.limit = 10;
            args.offset = 0;
            var _this = this;
            this.collection = new Collection({
                container: 'data-list',
                template: 'data-item',
                url: _global.url.api + 'agent/point_list',
                condition: args,
                model: {
                    tag_name: 'tr',
                    url: {
                        put: _global.url.api + 'record/{id}'
                        //   delete: _global.url.api + 'apply'
                    },

                    change: function(e) {
                        this.data[$(e.target).attr('name')] = $(e.target).val();
                    }
                },
                callback: function() {
                    loaded();
                    $('#today').html(_this.collection.result.point['day']);
                    $('#amounts').html(_this.collection.result.point['amounts']);
                }
            });
            this.collection.fetch();
        },

        event: function() {
            var _this = this;

            /**
             * 搜索事件
             */
            $('#btn-search').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.reload();
            });


            /**
             * 按状态查询
             */
            $('#btn-apply_status').find('a').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.keyword = '';
                _this.collection.condition.status = $(e.target).attr('rel');
                _this.collection.reload();
                $(e.target).addClass('active').siblings().removeClass('active');
            });
        },

        change: function(e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }

    };

    new index();

});